﻿using System;
using System.Web;
using Microsoft.SharePoint;

using Kiiro.Common.Utility;

namespace Kiiro.SiteDefinition.ApplicationPages
{
    public partial class KiiroError : Microsoft.SharePoint.ApplicationPages.ErrorPage
    {
        /// <summary>
        /// Process Error(s) and display to User.
        /// MUST override void OnLoad because using void Page_Load cause an infinite recursive loop.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad ( EventArgs e )
        {
            // set page using default SharePoint template
            base.OnLoad ( e );

            // overwrite with custom if passed in properly
            HttpContext ctx = HttpContext.Current;

            if ( ctx != null )
            {
                if ( ctx.Request.Params != null && ctx.Request.Params.Count > 0 )
                {
                    HttpRequest req = ctx.Request;

                    if ( req != null )
                    {
                        // get values form querystring
                        string ticksVal = req.Params [ "ticksVal" ];
                        Guid siteCollectionId = GetSiteCollectionId ( req );
                        string serverRelativeUrl = req.Params [ "servRelUrl" ];

                        if ( !string.IsNullOrEmpty ( ticksVal ) )
                        {
                            // get error details from cache
                            string cacheKeyName = "KiiroBasePageError" + ticksVal;
                            object cacheVarValue = null;

                            try
                            {
                                if ( siteCollectionId != default ( Guid ) )
                                    cacheVarValue = CacheHelper.GetCache ( cacheKeyName, siteCollectionId, serverRelativeUrl );
                            }
                            catch ( Exception ex )
                            {
                                ExceptionManagement.ExceptionManager.HandleException ( ex, true );
                            }

                            // set error display
                            if ( cacheVarValue != null && !string.IsNullOrEmpty ( cacheVarValue.ToString ( ) ) )
                            {
                                this.LinkContainedInMessage.Text = string.Empty;
                                this.LinkContainedInMessage.NavigateUrl = string.Empty;

                                // add custom data
                                this.litErrorInfo.Text = SPHelpers.FormatCommentText ( cacheVarValue.ToString ( ) );
                                this.litErrorExtendedDetails.Text = string.Empty;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// MUST override and remove call to base for Master Page and Kiiro look and feel to be rendered
        /// </summary>
        /// <param name="e"></param>
        protected override void OnError ( EventArgs e )
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        private static Guid GetSiteCollectionId ( HttpRequest req )
        {
            Guid siteCollectionId = Guid.Empty;

            try
            {
                string siteColl = req.Params [ "siteColl" ];
                siteCollectionId = new Guid ( siteColl );
            }
            catch { }

            if ( siteCollectionId == Guid.Empty && SPContext.Current != null && SPContext.Current.Site != null )
            {
                siteCollectionId = SPContext.Current.Site.ID; 
            }

            return siteCollectionId;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.ApplicationPages/ApplicationPages/KiiroError.aspx.cs $
 * 
 * 11    2/08/10 5:04p Camerons
 * 
 * 10    1/25/10 1:17p Camerons
 * 
 * 9     1/12/10 12:03p Camerons
 * 
 * 8     1/08/10 11:30a Camerons
 * 
 * 7     26/11/09 10:35a Camerons
 * 
 * 6     17/11/09 6:46p Camerons
 * 
 * 5     22/09/09 6:03p Camerons
 * Code changes to support creating user on demand - version 1.1.x.x
 * 
 * 4     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/